from x01_stock.xx_util.DBUtil import SqlHelper


def handle_biaoxian(datas,stat_date):
    """
    处理点击表现后返回的数据
    :param datas:
    :param stat_date:
    :return:
    """
    # print(data2)
    for data in datas:
        data = dict(sorted(data.items(), key=lambda item: item[0]))
        print(data)
        stock_code = ''
        sql = (
            'update stock_wencai_choumajizhong set nianchuzhijinzhangfu = %s ,jin10rizhangfu = %s,jin20rizhangfu = %s , jin3rizhangfu = %s,'
            'jin5rizhangfu = %s  ,jin1yuerizhangfu = %s,jin1nianzhangfu = %s,jin3yuezhangfu = %s    ')
        set_value = []
        for key in data.keys():
            if key == 'code' or key == 'market_code' or key == '最新价' or key == '最新涨跌幅' or key == '股票简称':
                continue
            elif key == '股票代码':
                stock_code = data.get(key)
            else:
                if type(data[key]) == float or type(data[key]) == int:
                    set_value.append(key + ':' + str(round(data.get(key), 2)))
                else:
                    set_value.append(key + ':' + data.get(key))
        print(set_value)
        sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
        print(sql)
        SqlHelper.get_instance().execute_curd(sql, set_value)


def handle_jishu(datas,stat_date):
    """
    处理点击技术后返回的数据
    :param datas:
    :param stat_date:
    :return:
    """
    for data in datas:
        data = dict(sorted(data.items(), key=lambda item: item[0]))
        print(data)
        stock_code = ''
        sql = ('update stock_wencai_choumajizhong set ddesanhu = %s ,geguredu = %s , zhulizijin = %s ,suoshugainian = %s, huanshoulv = %s ,'
               'jingjialiang = %s ,jingjiajin_e = %s ')
        set_value = []
        for key in data.keys():
            if key == 'code' or key == 'market_code' or key == '最新价' or key == '最新涨跌幅' or key == '股票简称' or key.startswith('技术形态') or key.startswith('集中度90'):
                continue
            elif key == '股票代码':
                stock_code = data.get(key)
            else:
                set_value.append(data.get(key))
        # print(set_value) 如果表格中有 -- 则返回的数据直接少了这个字段，导致更新数据失败。暂不处理
        sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
        print(sql)
        SqlHelper.get_instance().execute_curd(sql, set_value)

def handle_guzhi(datas,stat_date):
    """
    处理点击估值后返回的数据
    :param datas:
    :param stat_date:
    :return:
    """
    for data in datas:
        data = dict(sorted(data.items(), key=lambda item: item[0]))
        print(data)
        stock_code = ''
        sql = ('update stock_wencai_choumajizhong set ddesanhu = %s ,geguredu = %s , zhulizijin = %s ,suoshugainian = %s, huanshoulv = %s ,'
               'jingjialiang = %s ,jingjiajin_e = %s ')
        set_value = []
        for key in data.keys():
            if key == 'code' or key == 'market_code' or key == '最新价' or key == '最新涨跌幅' or key == '股票简称' or key.startswith('技术形态') or key.startswith('集中度90'):
                continue
            elif key == '股票代码':
                stock_code = data.get(key)
            else:
                set_value.append(data.get(key))
        # print(set_value) 如果表格中有 -- 则返回的数据直接少了这个字段，导致更新数据失败。暂不处理
        sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
        print(sql)
        SqlHelper.get_instance().execute_curd(sql, set_value)

def handle_guzhi(datas, stat_date):
    """
    处理点击资产负债后返回的数据
    :param datas:
    :param stat_date:
    :return:
    """
    for data in datas:
        data = dict(sorted(data.items(), key=lambda item: item[0]))
        print(data)
        stock_code = ''
        sql = (
            'update stock_wencai_choumajizhong set fuzhaiheji = %s ,zongzichan = %s , zichanfuzhailv = %s ')
        set_value = []
        for key in data.keys():
            if key == '股票代码':
                stock_code = data.get(key)
            elif key.startswith('负债合计') or key.startswith('资产总计') or key.startswith('资产负债率'):
                set_value.append(data.get(key))
            else:
                continue
        # print(set_value)  # 如果表格中有 -- 则返回的数据直接少了这个字段，导致更新数据失败。暂不处理
        sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
        # print(sql)
        SqlHelper.get_instance().execute_curd(sql, set_value)

def handle_guzhi(datas, stat_date):
    """
    处理点击资产负债后返回的数据
    :param datas:
    :param stat_date:
    :return:
    """
    for data in datas:
        data = dict(sorted(data.items(), key=lambda item: item[0]))
        print(data)
        stock_code = ''
        sql = ('update stock_wencai_choumajizhong set zichanjingshouyilv = %s ,jinglirun = %s, guanlifeiyong = %s , yingyechengben = %s ,  '
                            'yingyeshouru_tbzzl = %s,yingyeshouru = %s, caiwufeiyong = %s,xiaoshoumaolilv = %s,xiaoshoufeiyong = %s ')
        set_value = []
        for key in data.keys():
            if key == 'code' or key == 'market_code' or key == '最新价' or key == '最新涨跌幅' or key == '股票简称':
                continue
            elif key == '股票代码':
                stock_code = data.get(key)
            else:
                set_value.append(data.get(key))
        # print(set_value)  # 如果表格中有 -- 则返回的数据直接少了这个字段，导致更新数据失败。暂不处理
        sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
        # print(sql)
        SqlHelper.get_instance().execute_curd(sql, set_value)